﻿using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Text.RegularExpressions;

namespace DataProcessing.ThermoXRF
{
    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "Thermo(美国热电) 荧光仪"; } }

        public IDataOutput Processing(string content)
        {
            XRayOutputModel outputModel = new XRayOutputModel();
            var reItem = new Regex("[\\s]+").Replace(content, " ");
            //20170823,TEST -  ,SiO2,21.16,Al2O3,5.23,Fe2O3,3.11,
            string[] spdata = reItem.Split(',');

            if (DateTime.TryParseExact(spdata[0], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime date))
            {
                outputModel.MeasureTime = date;
            }
            else
            {
                outputModel.MeasureTime = DateTime.Now;
            }
            outputModel.SampleName = spdata[1];
            outputModel.AnalyzeApplicationName = spdata[2];

            for (int i = spdata.Length - 1; i > 2; i--)
            {
                if (spdata[i] != "" && double.TryParse(spdata[i], out double _))
                {
                    if (!outputModel.Eelements.ContainsKey(spdata[i - 1])) outputModel.Eelements.Add(spdata[i - 1], spdata[i]);
                    i--;
                }
            }
            return outputModel;
        }
    }
}
